<?php

class Competition {

    public $id;
    public $name;
    public $city;
    public $country;
    public $facility;
    public $pool;
    public $start_date;
    public $end_date;
    public $count_results;

    function __construct($name = '', $city = '', $country = '', $facility = '', $pool = '', $start_date = '', $end_date = '') {
        $this->name = $name;
        $this->city = $city;
        $this->country = $country;
        $this->facility = $facility;
        $this->pool = $pool;
        $this->start_date = $start_date;
        $this->end_date = $end_date;
    }

    /**
     * Load competition from db by ID;
     * @param type $id
     * @return Competition
     */
    public static function fromID($id) {
        $instance = new self();
        $instance->loadByID($id);
        return $instance;
    }

    public static function fromRow(array $row) {
        $instance = new self();
        $instance->fill($row);
        return $instance;
    }

    private function loadByID($id) {
        $result = mysql_query("SELECT `id`, `name`, `city`,`country`,`facility`, `pool`, `start_date`, `end_date` FROM `competitions` WHERE `id`='$id'");
        if (mysql_num_rows($result) == 0) {
            return null;
        }
        $row = mysql_fetch_assoc($result);
        $this->fill($row);
    }

    private function fill(array $row) {
        $this->id = $row['id'];
        $this->name = $row['name'];
        $this->city = $row['city'];
        $this->country = $row['country'];
        $this->facility = $row['facility'];
        $this->pool = $row['pool'];
        $this->start_date = $row['start_date'];
        $this->end_date = $row['end_date'];
    }

    public function toArray() {
        return (array($this->name, $this->city, $this->country, $this->facility, $this->pool, $this->start_date, $this->end_date));
    }

    public function getID() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getCity() {
        return $this->city;
    }

    public function setCity($city) {
        $this->city = $city;
    }

    public function getCountry() {
        return $this->country;
    }

    public function getCountryName() {
        return Utils::getCountryName($this->country);
    }

    public function setCountry($country) {
        $this->country = $country;
    }

    public function getFacility() {
        return $this->facility;
    }

    public function setFacility($facility) {
        $this->facility = $facility;
    }

    public function getPool() {
        return $this->pool;
    }

    public function setPool($pool) {
        $this->pool = $pool;
    }

    public function getStartDate() {
        return $this->start_date;
    }

    public function setStartDate($start_date) {
        $this->start_date = $start_date;
    }

    public function getEndDate() {
        return $this->end_date;
    }

    public function setEndDate($end_date) {
        $this->end_date = $end_date;
    }

    public function getCountResults() {
        return $this->count_results;
    }

    public function setCountResults($count_results) {
        $this->count_results = $count_results;
    }

    public function __toString() {
        return "name: $this->name, city: $this->city, city: $this->country, city: $this->facility, pool: $this->pool, 'start_date: $this->start_date, end_date: $this->end_date <br>";
    }

    public function toTableRow() {
        return "<tr><td>$this->name</td><td>$this->city</td><td>$this->country</td><td>$this->facility</td><td>$this->pool</td><td>$this->start_date</td><td>$this->end_date</td></tr>";
    }

    public function toTableColumns() {
        return "<td>$this->name</td><td>$this->city</td><td>$this->country</td><td>$this->facility</td><td>$this->pool</td><td>$this->start_date</td><td>$this->end_date</td>";
    }

}

?>
